home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10624 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP!! with file read/write
  5. Date: 19 Mar 1996 00:30:47 GMT
  6. Organization: Nando.net Public Access
  7. Message-ID: <4ikv7n$392@castle.nando.net>
  8. References: <4iijmh$brs@cloner2.ix.netcom.com>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: grail407.nando.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4iijmh$brs@cloner2.ix.netcom.com>, scoshe@ix.netcom.com(Christopher Scott Shelton ) writes:
  14. >
  15. >This part of my program should read a record, decrease daysleft by one,
  16. >and set today equal to zero and re-write the data at the original position.
  17. >For some reason it does one read, the following write and then it exits out.
  18. >I know that it should run 4 times.  Can anyone help me with this problem.
  19. >
  20. >void daily_maintainence()
  21. >{
  22. >  FILE *itemfile;
  23. >  long  filepos;
  24. >  struct _items item;
  25. >  itemfile=fopen("items.dat","r+b");          //open for read and write
  26. >  while (fread(&item,itemsize,1,itemfile)==1)
  27. >  {
  28. >    filepos=ftell(itemfile);                  //for debug
  29. >    item.today=0;                             //set to zero
  30. >    item.daysleft--;                          //decrement
  31. >    fseek(itemfile,-itemsize,SEEK_CUR);       //seek to where this rec starts
  32. >    filepos=ftell(itemfile);                  //for debug
  33. >    fwrite(&item,itemsize,1,itemfile);        //write it out
  34. >    filepos=ftell(itemfile);                  //for debug
  35. >  }
  36. >}
  37.  
  38. This code will not compile.  Your use of C++ style comments are syntax
  39. errors in C.  Without an appropriate include file, C doesn't know about
  40. FILE.  You have not defined itemsize.  You have declared, but not
  41. defined item (so item.today and item.daysleft are meaningless).
  42.  
  43. All this aside, an fread cannot follow an fwrite here without first
  44. performing a positioning function.  Perhaps adding the following
  45. after fwrite() will help:
  46.  
  47.    fseek( itemfile, 0, SEEK_CUR);
  48.  
  49. Bill McCarthy
  50. actuary@nando.net
  51. Wendell, NC  USA
  52.  
  53.